﻿using System.Collections.Generic;
using System.Dynamic;

namespace Eksponent.RazorRocks.Helpers
{
    public class DynamicDictionary : DynamicObject
    {
        private readonly IDictionary<string, object> _values;

        public DynamicDictionary(IDictionary<string, object> values)
        {
            _values = values;
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = _values.GetOrDefault(binder.Name);
            return true;
        }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            _values[binder.Name] = value;
            return true;
        }

        public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
        {
            result = _values.GetOrDefault(indexes[0] as string);
            return true;
        }

        public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value)
        {
            _values[indexes[0] as string] = value;
            return true;
        }

        
    }
}
